package com.project.core.util;

import com.common.exception.ErrCode;
import com.common.exception.SeException;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

@SuppressWarnings("CallToPrintStackTrace")
public class Word {
    /**
     * 读取word文件
     *
     * @param path 文件路径，物理路径，如"C:\\Users\\xxx\\Desktop\\tq.doc"
     * @return 文本
     */
    public static String readPath(String path) {
        String resullt = "";
        // 首先判断文件中的是doc/docx
        try {
            if (path.endsWith(".doc")) {
                InputStream is = new FileInputStream(path);
                WordExtractor extractor = new WordExtractor(is);
                resullt = extractor.getText();
                extractor.close();
                is.close();
            } else if (path.endsWith(".docx")) {
                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                resullt = extractor.getText();
                extractor.close();
                opcPackage.close();
            } else {
                System.out.println("此文件不是word文件");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SeException(ErrCode.System_Error, "读取word信息异常！");
        }
        return resullt;
    }

    /**
     * 读取word文件
     *
     * @param fileUrl 文件路径，网络路径，如 "https://www.baidu.com/file/1.doc"
     * @return 文本
     */
    public static String readUrl(String fileUrl) {

        String resullt = "";
        String tempFileUrl = "";
        // 判断文件中的是doc/docx
        // 首先判断是否有?后面的参数，有则要暂时去掉后获取到文件的后缀
        // 截取URL中问号后的内容
        int questionMarkIndex = fileUrl.indexOf("?");
        if (questionMarkIndex != -1) {
            tempFileUrl = fileUrl.substring(0, questionMarkIndex);
        }
        try {
            // 打开URL连接并获取输入流
            URL url = new URL(fileUrl);
            InputStream is = url.openStream();

            if (tempFileUrl.endsWith(".doc")) {
                WordExtractor extractor = new WordExtractor(is);
                resullt = extractor.getText();
                extractor.close();
                is.close();
            } else if (tempFileUrl.endsWith(".docx")) {
                XWPFDocument document = new XWPFDocument(is);
                // 使用 XWPFWordExtractor 提取文档内容
                XWPFWordExtractor extractor = new XWPFWordExtractor(document);
                resullt = extractor.getText();
                document.close();
                extractor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SeException(ErrCode.System_Error, "读取word信息异常！");
        }
        return resullt;
    }
}
